home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / comm2 / s342q08.lha / sysdoor.c < prev    next >
C/C++ Source or Header  |  1995-09-16  |  17KB  |  602 lines

  1. /************************************************************************/
  2. /*                           SysDoor.c                                  */
  3. /*                                                                      */
  4. /*      Handles user-interface for doors within Citadel.  Note this is  */
  5. /*      a system dependent file, not necessarily portable.              */
  6. /************************************************************************/
  7. #define SYSTEM_DEPENDENT
  8. /* #define NO_DOORS */
  9. #include "ctdl.h"
  10. #ifndef NO_DOORS
  11. #include "c68door.h"
  12. /************************************************************************/
  13. /*                              History                                 */
  14. /*                                                                      */
  15. /* 92Sep18  AFP re-wrote validity checks                                */
  16. /* 89Nov24  AP  Extensively modified for Amiga                          */
  17. /* 89Jan30  HAW AutoDoors.                                              */
  18. /* 89Jan21  HAW Controlled access via #events                           */
  19. /* 88Dec16  HAW Created.                                                */
  20. /************************************************************************/
  21.  
  22. char Legal_Door(DoorData *, int, int, int); /* door validity checker */
  23. void Execute_Door(DoorData *,int);             /* execute current door  */
  24. char MakeCmdLine(char *, char *, char *, int);
  25. char ShowDoors(FILE *fd);
  26. int BaudCode(int);
  27.  
  28. /************************************************************************/
  29. /*      Local variables                                                 */
  30. /************************************************************************/
  31. static SListBase DoorTime =
  32.   {
  33.   NULL, ChkTwoNumbers, NULL, free, EatTwoNumbers
  34.  
  35.   };
  36. static char      *BpsStr;
  37. long      DoorsUsed = 0l;
  38. extern int       *lPtrTab;
  39. extern int       UngotoStack[];
  40. extern CONFIG    cfg;
  41. extern MessageBuffer   msgBuf;
  42. extern aRoom     roomBuf;
  43. extern logBuffer logBuf;
  44. extern long      *DL_Total;
  45. extern long      byteRate;
  46. extern char      onConsole;
  47. extern int       thisLog;
  48. extern int       thisRoom;
  49. extern char      ForceNet;
  50. extern char      loggedIn;
  51. extern char      remoteSysop;
  52. extern char      CallSysop;
  53. extern char      BpsSet;
  54. extern int       SystemPort;
  55. extern SListBase DL_List;
  56. char line[120];               /* static line buffer */
  57. /************************************************************************/
  58. /*      doDoor() chief administrator for doors                          */
  59. /************************************************************************/
  60. char doDoor(char moreYet)
  61.   {
  62.   char        DoorId[6], match = FALSE;
  63.   int         Count = -1;
  64.   FILE        *fd;
  65.   SYS_FILE    name;
  66.   DoorData    DoorInfo;
  67.   extern char *READ_ANY, *WRITE_ANY, outFlag;
  68.   extern int  exitValue;
  69.   extern char ExitToMsdos, *WRITE_ANY;
  70.   extern FILE *upfd;
  71.   extern long Door_Limit;
  72.   /* First handle simple privilege check */
  73.   if (!DoorPriv)
  74.     {
  75.     Output_Citadel_Message("NODORP", NULL, NULL, NULL);
  76.     return GOOD_SELECT;
  77.  
  78.     }
  79.   if (Door_Limit_On() && !HalfSysop() && DoorsUsed / 60 >= Door_Limit)
  80.     {
  81.     Output_Citadel_Message("NODORT", NULL, NULL, NULL);
  82.     return GOOD_SELECT;
  83.  
  84.     }
  85.   /* see if there are any doors present. */
  86.   makeSysName(name, DOOR_DATA, &cfg.roomArea);
  87.   if ((fd = safeopen(name, READ_ANY)) == NULL)
  88.     {
  89.     Output_Citadel_Message("NODORF", NULL, NULL, NULL);
  90.     return GOOD_SELECT;
  91.  
  92.     }
  93.   /*
  94.   * If moreYet, then wait for name.  If not, then print out current
  95.   * doors and prompt for name.
  96.   */
  97.   if (!moreYet)
  98.   if (!ShowDoors(fd))
  99.     {
  100.     Output_Citadel_Message("NODORF", NULL, NULL, NULL);
  101.     fclose(fd);
  102.     return GOOD_SELECT;
  103.  
  104.     }
  105.   if (getNormStr((moreYet)?"":"DOORID", DoorId, 6,
  106.   ((moreYet) ? BS_VALID : 0) | QUEST_SPECIAL) == BACKED_OUT) return BACKED_OUT;
  107.   if (DoorId[0] == '?')
  108.     {
  109.     ShowDoors(fd);
  110.     fclose(fd);
  111.     return GOOD_SELECT;
  112.  
  113.     }
  114.   if(strLen(DoorId) != 0)
  115.     {
  116.     while (!match && fread((char *)&DoorInfo, sizeof DoorInfo, 1, fd) != 0)
  117.       {
  118.       if(match = (strCmpU(DoorId, DoorInfo.entrycode) == SAMESTRING))
  119.         {
  120.         match =  Legal_Door(&DoorInfo,FALSE,FALSE,FALSE);
  121.  
  122.         };
  123.       Count++;
  124.  
  125.       }
  126.     if( !match )
  127.       {
  128.       Output_Citadel_Message("NODRFD", (long)DoorId, NULL, NULL);
  129.  
  130.       }
  131.     else
  132.       {
  133.       if( NoTimeForDoor(Count, &DoorInfo) )
  134.         {
  135.         Output_Citadel_Message("NODORT", NULL, NULL, NULL);
  136.  
  137.         }
  138.       else
  139.         {
  140.         Execute_Door(&DoorInfo,Count);
  141.  
  142.         };
  143.  
  144.       };
  145.  
  146.     };
  147.   fclose(fd);
  148.   return GOOD_SELECT;
  149.  
  150.   }
  151. /************************************************************************/
  152. /*      RunAutoDoor() does actual work of running a door                */
  153. /************************************************************************/
  154. char RunAutoDoor(int i, char ask)
  155.   {
  156.   FILE *fd;
  157.   SYS_FILE name;
  158.   DoorData   drBuf;
  159.   if (i == -1) return FALSE;
  160.   makeSysName(name, DOOR_DATA, &cfg.roomArea);
  161.   if ((fd = safeopen(name, READ_ANY)) == NULL)
  162.     {
  163.     splitF(NULL,"Internal error:no %s door data file!\n ",name);
  164.     return FALSE;
  165.  
  166.     };
  167.   if (fseek(fd, sizeof drBuf * i, 0) != 0)
  168.     {
  169.     splitF(NULL,"Couldn't seek to %d in the doors file!\n", i);
  170.     fclose(fd);
  171.     return FALSE;
  172.  
  173.     };
  174.   if (fread((char *)&drBuf, sizeof(drBuf), 1, fd) <= 0)
  175.     {
  176.     splitF(NULL,"Couldn't read from the doors file!\n");
  177.     splitF(NULL," Filename:%s Position:%d size: %d byte offset: %ld\n",
  178.     name,i, sizeof(drBuf),(long)i*sizeof(drBuf));
  179.     fclose(fd);
  180.     return FALSE;
  181.  
  182.     };
  183.   fclose(fd);
  184.   if( !Legal_Door(&drBuf,ask,TRUE,FALSE) )return TRUE;
  185.   /*
  186.   ** got a legal door now execute it
  187.   */
  188.   Execute_Door(&drBuf,i);
  189.   return TRUE;
  190.   }
  191.  
  192. int UnQueueSerRead(void);
  193. int QueueSerRead(int);
  194. int Jsystem(char *);
  195.  
  196. void Execute_Door(dr,i)
  197. DoorData   *dr;
  198. int i;
  199.   {
  200.   SYS_FILE    name;
  201.   long *AlreadyUsed, Before, After;
  202.   TwoNumbers  *temp;
  203.   int  byr, bday, bhour, bmin;
  204.   int  ayr, aday, ahour, amin;
  205.   char *bmon, *amon;
  206.   sPrintf(line, "%s ", dr->program);
  207.   MakeCmdLine(lbyte(line), dr->parameters, "", sizeof line - strlen(line));
  208.   /*
  209.   ** if auditing enabled, collect statistics
  210.   */
  211.   if( cfg.Audit )
  212.     {
  213.     getCdate(&byr, &bmon, &bday, &bhour, &bmin);
  214.  
  215.     };
  216.   chdir(dr->location);  /* set the directory */
  217.   UnQueueSerRead();       /* clear the serial port */
  218.   Before = CurAbsolute();
  219.   SpecialMessage("Status:Door Execution");
  220.   Jsystem(line);          /* execute the command line */
  221.   After = CurAbsolute();
  222.   QueueSerRead(6);        /* reset the serial port with a read */
  223.   homeSpace();            /* reset to the home directory */
  224.   if (cfg.Audit)
  225.     {
  226.     getCdate(&ayr, &amon, &aday, &ahour, &amin);
  227.     sPrintf(line,
  228.     "    %20s used %6s %2d%s%02d %d:%02d - %d:%02d (%ld:%02ld)",
  229.     logBuf.lbname, dr->entrycode, byr, bmon, bday,
  230.     bhour, bmin, ahour, amin, ((After - Before) / 60l),
  231.     ((After - Before) % 60l));
  232.     makeAuditName(name, "dooruse.sys");
  233.     CallMsg(name, line);
  234.  
  235.     };
  236.   temp = (TwoNumbers *) GetDynamic(sizeof *temp);
  237.   temp->first = i;
  238.   AlreadyUsed = (long *) SearchList(&DoorTime, temp);
  239.   if (AlreadyUsed != NULL)
  240.   temp->second = (*AlreadyUsed) + (CurAbsolute() - Before);
  241.   else temp->second = CurAbsolute() - Before;
  242.   AddData(&DoorTime, temp, NULL, TRUE);
  243.  
  244.   }
  245. #ifdef REFERENCE
  246. char  entrycode[6];         /* What user uses to specify a door     */
  247. char  program[14];          /* name of program                      */
  248. char  location[50];         /* door location                        */
  249. char  description[80];      /* description of door                  */
  250. char  flags;                /* sysop, aides, or everyone            */
  251. char  parameters[100];      /* list of parameters.                  */
  252. int   TimeLimit;            /* time limit for this door             */
  253. label RoomName;             /* room name                            */
  254. #endif
  255. /************************************************************************/
  256. /*      ShowDoors() Show the doors about                                */
  257. /************************************************************************/
  258. char ShowDoors(FILE *fd)
  259.   {
  260.   DoorData DoorInfo;
  261.   int      DoorCount = 0;
  262.   while (fread((char *)&DoorInfo, sizeof DoorInfo, 1, fd) != 0 && outFlag == OUTOK)
  263.     {
  264.     if( Legal_Door(&DoorInfo,FALSE,FALSE,FALSE) )
  265.       {
  266.       DoorCount++;
  267.       doCR();
  268.       mPrintf("%-10s: %s", DoorInfo.entrycode, DoorInfo.description);
  269.  
  270.       };
  271.  
  272.     }
  273.   doCR();
  274.   fseek(fd, 0l, 0);
  275.   return (char)(DoorCount != 0);
  276.  
  277.   }
  278. /************************************************************************/
  279. /*      BackFromDoor() checks to see if we are returning from a door    */
  280. /************************************************************************/
  281. char BackFromDoor()
  282.   {
  283.   return FALSE;
  284.  
  285.   }
  286. /************************************************************************/
  287. /*  NoTimeForDoor() see if there is still time to run the door. */
  288. /************************************************************************/
  289. char NoTimeForDoor(int which, DoorData *DoorInfo)
  290.   {
  291.   TwoNumbers search;
  292.   long        *found;
  293.   if (DoorInfo->TimeLimit == -1) return FALSE;
  294.   search.first = which;
  295.   if ((found = (long *) SearchList(&DoorTime, &search)) != NULL)
  296.   return (char)((*found) / 60l >= (long) DoorInfo->TimeLimit);
  297.   return (char)FALSE;
  298.  
  299.   }
  300. /************************************************************************/
  301. /*  ClearDoorTimers() clear the door timers list.     */
  302. /************************************************************************/
  303. void ClearDoorTimers()
  304.   {
  305.   KillList(&DoorTime);
  306.  
  307.   }
  308. /************************************************************************/
  309. /*  Cumulate() Find out how much time has been used by current user */
  310. /************************************************************************/
  311. void Cumulate(TwoNumbers *temp)
  312.   {
  313.   DoorsUsed += temp->second;          /* cumulate it ... */
  314.  
  315.   }
  316. /************************************************************************/
  317. /*  DoorHelpListing() list the doors for the help system    */
  318. /************************************************************************/
  319. void DoorHelpListing(char *target)
  320.   {
  321.   DoorData DoorInfo;
  322.   SYS_FILE name;
  323.   int count = 0;
  324.   FILE     *fd;
  325.   /* see if there are any doors present. */
  326.   makeSysName(name, DOOR_DATA, &cfg.roomArea);
  327.   if ((fd = safeopen(name, READ_ANY)) == NULL)
  328.     {
  329.     sPrintf(target, "    There are no doors available.\n");
  330.     return;
  331.  
  332.     }
  333.   target[0] = 0;
  334.   while (fread((char *)&DoorInfo, sizeof DoorInfo, 1, fd) != 0 && outFlag == OUTOK)
  335.     {
  336.     if( Legal_Door(&DoorInfo,FALSE,FALSE,FALSE) )
  337.       {
  338.       count++;
  339.       sPrintf(lbyte(target), "   %-10s: %s\n", DoorInfo.entrycode, DoorInfo.description);
  340.  
  341.       }
  342.  
  343.     }
  344.   fclose(fd);
  345.   if (count == 0)sPrintf(target, "    There are no doors available.\n");
  346.  
  347.   }
  348. /************************************************************************/
  349. /*  ReadBps() read the bps off the command line.      */
  350. /************************************************************************/
  351. long Get_CPS(long, char *);
  352. void ReadBps(char *str)
  353.   {
  354.   short i;
  355.   extern char *rates[];   /* so we can generate the baud rate string. */
  356.   BpsStr = str + 4;
  357.   if (strCmpU(BpsStr, "LOCAL") == SAMESTRING ||
  358.   strCmpU(BpsStr, "0") == SAMESTRING) byteRate = 0;
  359.   else
  360.     {
  361.     byteRate = Get_CPS(atoi(BpsStr), "ReadBps");
  362.     if( (i = BaudCode(byteRate)) == -1 )
  363.       {
  364.       if( ( i = BaudCode(byteRate/10)) == -1 )
  365.         {
  366.         printf("Unrecognized Baud/BPS rate on command line.\n");
  367.         return;
  368.  
  369.         };
  370.  
  371.       };
  372.     logMessage(BAUD, rates[i], FALSE);
  373.  
  374.     };
  375.  
  376.   }
  377. /************************************************************************/
  378. /*  SetUpPort() setup the port appropriately.     */
  379. /************************************************************************/
  380. int SetUpPort(int bps)
  381.   {
  382.   int code;
  383.   bps = byteRate = Get_CPS(bps,"SetUpPort:A");
  384.   code = BaudCode(byteRate);
  385.   if (code == -1)
  386.     {
  387.     printf("door error, unrecognizable bps.\n");
  388.     code = 0;
  389.  
  390.     }
  391.   CitadelBaudRate(code, "SetUpPort:B");
  392.   return code;
  393.  
  394.   }
  395. /************************************************************************/
  396. /*  BaudCode() find out what our baud code is     */
  397. /************************************************************************/
  398. int BaudCode(int bps)
  399.   {
  400.   switch (bps)
  401.     {
  402.     case    0:   return (int)cfg.sysBaud;
  403.     case   30:   return 0;
  404.     case  120:   return 1;
  405.     case  240:   return 2;
  406.     case  480:   return 3;
  407.     case  960:   return 4;
  408.     case 1440:   return 5;
  409.     case 1920:   return 6;
  410.     case 3840:   return 7;
  411.     case 5760:   return 8;
  412.     default: return -1;
  413.  
  414.     }
  415.  
  416.   }
  417. /************************************************************************/
  418. /*  NewUserDoor() called as new user identifies himself    */
  419. /************************************************************************/
  420. char NewUserDoor()
  421.   {
  422.   FILE        *fd;
  423.   SYS_FILE    name;
  424.   DoorData    DoorInfo;
  425.   int not_found;
  426.   int more;
  427.   extern char *READ_ANY, *WRITE_ANY, outFlag;
  428.   /* get door data */
  429.   makeSysName(name, DOOR_DATA, &cfg.roomArea);
  430.   if ((fd = safeopen(name, READ_ANY)) == NULL)
  431.     {
  432.     Output_Citadel_Message("NODORF", NULL, NULL, NULL);
  433.     }
  434.   else
  435.     {
  436.     more = not_found = TRUE;
  437.     while( more && not_found)
  438.       {
  439.       more = fread((char *)&DoorInfo, sizeof DoorInfo, 1, fd) != 0;
  440.       if( more )
  441.         {
  442.         not_found =  ! Legal_Door(&DoorInfo,FALSE,FALSE,TRUE);
  443.  
  444.         };
  445.  
  446.       };
  447.     fclose(fd);
  448.     if( ! not_found )
  449.       {
  450.       sPrintf(line, "%s ", DoorInfo.program);
  451.       MakeCmdLine(lbyte(line), DoorInfo.parameters, ""
  452.       , sizeof line - strlen(line));
  453.       UnQueueSerRead();
  454.       chdir(DoorInfo.location);  /* set the directory */
  455.       SpecialMessage("Status:NewUser Door Execution");
  456.       Jsystem(line);
  457.       QueueSerRead(6);
  458.       homeSpace();
  459.  
  460.       };
  461.  
  462.     };
  463.   return((char)TRUE);
  464.  
  465.   }
  466. char Flag_Test(int,int);
  467. char Flag_Test(flag,mask)
  468. int flag,mask;
  469.   {
  470.   int result;
  471.   result = flag & mask;
  472.   if (cfg.BoolFlags.debug)
  473.     {
  474.     splitF(NULL,"flag:%x, mask: %x result: %x\n",flag,mask,result );
  475.  
  476.     };
  477.   if( result == 0 )return TRUE;
  478.   return FALSE;
  479.   }
  480.  
  481. char Legal_Door(dr,verbose,autolegal,newuser)
  482. DoorData *dr;
  483. int verbose;    /* flag - give User a text reason */
  484. int autolegal;  /* are we allowing an autodoor? */
  485. int newuser;    /* are we allowing a new user door here?*/
  486.   {
  487.   char status;
  488.   int  reason;
  489.   status = TRUE;
  490.  
  491.   reason = 0;
  492.   /* Checks to see if this is a legal door in this case  */
  493.   if (!DoorPriv && !newuser)
  494.     {
  495.     if (cfg.BoolFlags.debug)
  496.       {
  497.       splitF(NULL,"No door priviledges\n");
  498.       };
  499.     reason |= 128;
  500.     status = FALSE;
  501.     };
  502.   if( dr->flags == DOOR_ANYONE ) return status;  /* anyone can run */
  503.   if( onConsole )
  504.     {
  505.     if(  Flag_Test(dr->flags, DOOR_CON) )
  506.       {
  507.       if (cfg.BoolFlags.debug)
  508.         {
  509.         splitF(NULL,"Not able to run door from console.\n");
  510.         };
  511.       reason |= 1;
  512.       status = FALSE;
  513.       };
  514.     }
  515.   else
  516.     {
  517.     if( Flag_Test(dr->flags, DOOR_MODEM) )
  518.       {
  519.       if (cfg.BoolFlags.debug)
  520.         {
  521.         splitF(NULL,"Not able to run door from modem.\n");
  522.         };
  523.       reason |= 2;
  524.       status = FALSE;
  525.       };
  526.     };
  527.   if( !aide )
  528.     {
  529.     if( ! Flag_Test(dr->flags, DOOR_AIDE) )
  530.       {
  531.       if(cfg.BoolFlags.debug || verbose )
  532.         mPrintf("Sorry you must have AIDE privileges to run that door.\n");
  533.       reason |= 4;
  534.       status = FALSE;
  535.  
  536.       };
  537.  
  538.     };
  539.   if( !SomeSysop() )
  540.     {
  541.     if( ! Flag_Test(dr->flags, DOOR_SYSOP) )
  542.       {
  543.       if( cfg.BoolFlags.debug || verbose )
  544.         mPrintf("Sorry only the Sysop may run that door.\n");
  545.       reason |= 8;
  546.       status = FALSE;
  547.  
  548.       };
  549.  
  550.     };
  551.   if( autolegal )
  552.     {
  553.     if( Flag_Test(dr->flags,DOOR_AUTO))
  554.       {
  555.       if(cfg.BoolFlags.debug || verbose )
  556.         mPrintf("Error: That door is not an AutoDoor.\n");
  557.       reason |= 16;
  558.       status = FALSE;
  559.       };
  560.     }
  561.   else
  562.     {
  563.     if( !Flag_Test(dr->flags,DOOR_AUTO) )
  564.       {
  565.       if( cfg.BoolFlags.debug ||  verbose )
  566.         mPrintf("Sorry that is an AutoDoor.\n");
  567.       reason |= 16;
  568.       status = FALSE;
  569.  
  570.       };
  571.     };
  572.   if(  ( !newuser && !Flag_Test(dr->flags, DOOR_NEWUSER) )
  573.   ||   (  newuser &&  Flag_Test(dr->flags, DOOR_NEWUSER) ) )
  574.     {
  575.     if( cfg.BoolFlags.debug || verbose )
  576.       mPrintf("Sorry that is a new user door.\n");
  577.     reason |= 32;
  578.     status = FALSE;
  579.  
  580.     };
  581.   if( dr->RoomName[0] != '\0'
  582.   &&  strCmpU(dr->RoomName,roomBuf.rbname) != SAMESTRING )
  583.     {
  584.     if( verbose )mPrintf("Sorry you may only run that door from the %s room.\n",dr->RoomName);
  585.     if (cfg.BoolFlags.debug)
  586.       {
  587.       splitF(NULL," Door Name: %s =?= Room Name:%s\n",dr->RoomName,roomBuf.rbname);
  588.  
  589.       };
  590.     reason |= 64;
  591.     status = FALSE;
  592.  
  593.     };
  594.   if (cfg.BoolFlags.debug)
  595.     {
  596.     splitF(NULL,"%s  Legal Door:%s Reason %x\n",dr->program, ( reason ? " " : " NOT"),reason );
  597.  
  598.     };
  599.   return status;
  600.  
  601.   }
  602.